<?php
  if ($_REQUEST["bindata"] === NULL) { 
    echo "missing parameter.";
  }
  else {
    $img_data = base64_decode($_REQUEST["bindata"]);
    $name = $_REQUEST["name"] === NULL ? "anonymous" : $_REQUEST["name"];
    $name = strip_tags($name);
    $comment = strip_tags($_REQUEST["comment"]);
	$id = strip_tags($_REQUEST["id"]);
    $img_size = strlen($img_data);
    if ($img_size < 100000) {
    	
      $img_filename = "data/snapshot.jpg";
      $comment_filename = "data/comment.txt";
      //unlink($img_filename);
      //unlink($comment_filename);
      //$img_file = fopen($img_filename, "w") or die("can't open file");
	  $imgData =addslashes($img_data);
      //fwrite($img_file, $img_data);
      //fclose($img_file);
      echo "$img_size bytes uploaded.";
      // write comments
      var_dump($comment);
      $comment_lines = explode("\r", $comment);
      var_dump($comment_lines);
      //$comment_file = fopen($comment_filename, "w");
      //fwrite($comment_file, $name);
      //foreach ($comment_lines as $line) {
      //  fwrite($comment_file, $line . "\n");
      //}
      //fwrite($comment_file, $comment);
      //fclose($comment_file);
	  
      // put the image in the db...
      // database connection
	  mysql_connect("localhost", "root", "") OR DIE (mysql_error());
 
      // select the db
      mysql_select_db ("mb_f7") OR DIE ("Unable to select db".mysql_error());
 
      // our sql query
      $sql = "INSERT INTO player_pic
                ( image_id , image , image_name)
                VALUES
                (0,'${imgData}','0.jpg')";
				
	if(!mysql_query($sql)) 
		{
            echo 'Unable to upload file';
		}
	  
    }
    else {
      echo "image too big.";
    }
    echo "$name";
	echo "$comment";
  }
  //var_dump($_REQUEST);
?>

